Class can implement multiple interfaces using keyword implements followed by the comma separated list of Interfaces.
Interface is declared by
● using Keyword interface interface
● followed by Interface Name InterfaceA
● followed by Interface Body which can have
Methods abstract void abstractMethod() (abstract, static, default)
Syntax
interface InterfaceA { }
interface InterfaceB { }
class Person implements InterfaceA, InterfaceB { }
Syntax
//===========================================================================================================
// INTERFACE: InterfaceA
//===========================================================================================================
interface InterfaceA {
abstract void abstractMethod();
static void staticMethod () { System.out.println("staticMethod()"); }
}
//===========================================================================================================
// INTERFACE: InterfaceB
//===========================================================================================================
interface InterfaceB {
default void defaultMethod1() { System.out.println("defaultMethod1()"); }
default void defaultMethod2() { System.out.println("defaultMethod2()"); }
}
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person implements InterfaceA, InterfaceB {
public void abstractMethod() { System.out.println("Overriden abstractMethod()" ); }
public void defaultMethod1() { System.out.println("Overriden defaultMethod1()" ); }
}
//===========================================================================================================
// CLASS: Test
//===========================================================================================================
public class Test {
public static void main(String args[]) {
//CLASS
Person john = new Person();
john.abstractMethod();
john.defaultMethod1();
john.defaultMethod2();
//INTERFACE
InterfaceA.staticMethod();
}
}